using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Windows.Forms;

namespace SIMc
{
    public partial class XMLProfiles
    {

        public string path;
        XmlDocument xmlDoc;

        public XMLProfiles(string appPath)
        {
            this.path = appPath + "profileList.xml";
            xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(path);
            }
            catch (Exception e)
            {
                createProfileList();
            }
        }

        public Profile getProfile(string accountName)
        {
            Profile profil = new Profile(0);

            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if (lNode.FirstChild.NextSibling.InnerText.Equals(accountName))
                {
                    foreach (XmlNode gNode in lNode.ChildNodes)
                    {
                        switch (gNode.Name)
                        {

                            case "profile_name":
                                profil.profile_name = gNode.InnerText;
                                break;
                            case "profile_password":
                                profil.profile_password = gNode.InnerText;
                                break;
                            case "remember":
                                profil.remember = bool.Parse(gNode.InnerText);
                                break;
                            case "default_acc":
                                profil.default_acc = int.Parse(gNode.InnerText);
                                break;
                            case "ID":
                                profil.ID = int.Parse(gNode.InnerText);
                                break;
                            case "account":
                                addAccount(profil, gNode);
                                break;
                        }
                    }
                }
            }
            return profil;
        }

        internal Account addAccount(Profile profil, XmlNode accNode){
            Account ucet= new Account();

            ucet.id = int.Parse(accNode.Attributes.Item(0).Value);
            foreach (XmlNode subNode in accNode.ChildNodes){
                switch (subNode.Name){
                    case "realm":
                        ucet.realm = subNode.InnerText;
                        break;
                    case "proxy":
                        ucet.proxy = subNode.InnerText;
                        break;
                    case "uri":
                        ucet.uri = subNode.InnerText;
                        break;
                    case "password":
                        ucet.password = subNode.InnerText;
                        break;
                    case "registrar":
                        ucet.registrar = subNode.InnerText;
                        break;
                    case "use":
                        ucet.use = bool.Parse(subNode.InnerText);
                        break;
                }
            }
            profil.setAccount(ucet, ucet.id);
            return ucet;
        }

        internal ArrayList getProfileNames()
        {
            ArrayList zoznam = new ArrayList();

            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if (lNode.Name.Equals("profile"))
                {
                    zoznam.Add(lNode.FirstChild.NextSibling.InnerText);
                }
            }
            return zoznam;
        }


        internal Profile updateProfile(Profile profil)
        {

            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if (int.Parse(lNode.FirstChild.InnerText) == profil.ID)
                {
                    foreach (XmlNode gNode in lNode.ChildNodes)
                    {
                        switch (gNode.Name)
                        {
                            case "ID":
                                gNode.InnerText = profil.ID.ToString();
                                break;
                            case "profile_name":
                                gNode.InnerText = profil.profile_name;
                                break;
                            case "profile_password":
                                gNode.InnerText = profil.profile_password;
                                break;
                            case "remember":
                                gNode.InnerText = profil.remember.ToString();
                                break;
                            case "default_acc":
                                gNode.InnerText = profil.default_acc.ToString();
                                break;
                            case "account":
                                updateAccount(profil, gNode);
                                break;
                        }
                    }
                    break;
                }
            }
            xmlDoc.Save(path);
            return profil;
        }

        internal void updateAccount(Profile profil, XmlNode accNode){
            int accIndex = int.Parse(accNode.Attributes.Item(0).Value);

            foreach(XmlNode subnode in accNode.ChildNodes){
                switch(subnode.Name){
                    case "proxy":
                        subnode.InnerText = profil.accounts[accIndex].proxy;
                        break;
                    case "uri":
                        subnode.InnerText = profil.accounts[accIndex].uri;
                        break;
                    case "realm":
                        subnode.InnerText = profil.accounts[accIndex].realm;
                        break;
                    case "password":
                        subnode.InnerText = profil.accounts[accIndex].password;
                        break;
                    case "registrar":
                        subnode.InnerText = profil.accounts[accIndex].registrar;
                        break;
                    case "use":
                        subnode.InnerText = profil.accounts[accIndex].use.ToString();
                        break;
                }
            }
        }

        internal bool deleteProfile(Profile profil)
        {

            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {
                if (int.Parse(lNode.FirstChild.InnerText) == profil.ID)
                {
                    xmlDoc.DocumentElement.RemoveChild(lNode);
                }
            }
            xmlDoc.Save(path);
            return true;
        }


        internal int addprofile(string profile_name, string profile_password, bool remember)
        {
            //XmlDocument xmlDoc= new XmlDocument();
            /*try
            {
                xmlDoc.Load(path);
            }
            catch (Exception e)
            {
                createProfileList();
            }
            */
            int topID = 0;
            foreach (XmlNode lNode in xmlDoc.DocumentElement.ChildNodes)
            {

                if (lNode.FirstChild.Name.Equals("ID"))
                {
                    if (int.Parse(lNode.FirstChild.InnerText) > topID)
                        topID = int.Parse(lNode.FirstChild.InnerText);
                }
            }
            topID++;

            //pridanie profile ako potomka rootu
            XmlElement rootNode = xmlDoc.DocumentElement;
            XmlElement lProfileElem = xmlDoc.CreateElement("profile");
            rootNode.AppendChild(lProfileElem);

            XmlElement lIDElem = xmlDoc.CreateElement("ID");
            XmlElement lProfileNameElem = xmlDoc.CreateElement("profile_name");
            XmlElement lProfilePassElem = xmlDoc.CreateElement("profile_password");
            XmlElement lRememberElem = xmlDoc.CreateElement("remember");
            XmlElement lDefaultAccElem = xmlDoc.CreateElement("default_acc");

            XmlText IDText = xmlDoc.CreateTextNode(topID.ToString());
            lIDElem.AppendChild(IDText);
            lProfileNameElem.InnerText = profile_name;
            lProfilePassElem.InnerText = profile_password;
            lRememberElem.InnerText = remember.ToString();
            lDefaultAccElem.InnerText = "0";

            lProfileElem.AppendChild(lIDElem);
            lProfileElem.AppendChild(lProfileNameElem);
            lProfileElem.AppendChild(lProfilePassElem);
            lProfileElem.AppendChild(lRememberElem);
            lProfileElem.AppendChild(lDefaultAccElem);

            for (int i = 0; i < 4; i++)
            {
                XmlElement lAccountElem = xmlDoc.CreateElement("account");

                XmlElement lURIElem = xmlDoc.CreateElement("uri");
                XmlElement lPassElem = xmlDoc.CreateElement("password");
                XmlElement lRegistrarElem = xmlDoc.CreateElement("registrar");
                XmlElement lRealmElem = xmlDoc.CreateElement("realm");
                XmlElement lProxyElem = xmlDoc.CreateElement("proxy");
                XmlElement lUseElem = xmlDoc.CreateElement("use");

                XmlText uriText = xmlDoc.CreateTextNode("");
                XmlText passText = xmlDoc.CreateTextNode("");
                XmlText registrarText = xmlDoc.CreateTextNode("");
                XmlText realmText = xmlDoc.CreateTextNode("");
                XmlText proxyText = xmlDoc.CreateTextNode("");
                XmlText useText = xmlDoc.CreateTextNode("false");

                lProfileElem.AppendChild(lAccountElem);

                lAccountElem.AppendChild(lURIElem);
                lAccountElem.AppendChild(lPassElem);
                lAccountElem.AppendChild(lRegistrarElem);
                lAccountElem.AppendChild(lRealmElem);
                lAccountElem.AppendChild(lProxyElem);
                lAccountElem.AppendChild(lUseElem);

                lURIElem.AppendChild(uriText);
                lPassElem.AppendChild(passText);
                lRegistrarElem.AppendChild(registrarText);
                lRealmElem.AppendChild(realmText);
                lProxyElem.AppendChild(proxyText);
                lUseElem.AppendChild(useText);
                //ulozenie

                //pridanie atributu
                XmlAttribute accID = xmlDoc.CreateAttribute("ID");
                accID.Value = i.ToString();
                lAccountElem.Attributes.Append(accID);
            }

            xmlDoc.Save(path);
            return topID;

        }


        internal bool createProfileList()
        {
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "no");

            XmlElement rootNode = xmlDoc.CreateElement("Profiles");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);

            // vytvorime 6 zaznamov
            for (int i = 1; i < 7; i++)
            {
                addprofile("rv_user" + i, "rv", true);
            }

            xmlDoc.Save(path);
            return true;
        }

        public void fillSettingsForm(SettingsForm settingsForm, Profile profil){
            //acc1
            settingsForm.TB_acc1_acc.Text = profil.accounts[0].uri;
            settingsForm.TB_acc1_pass.Text = profil.accounts[0].password;
            settingsForm.TB_acc1_proxy.Text = profil.accounts[0].proxy;
            settingsForm.TB_acc1_realm.Text = profil.accounts[0].realm;
            settingsForm.TB_acc1_registrar.Text = profil.accounts[0].registrar;
            settingsForm.CB_acc1_use.Checked = profil.accounts[0].use;
            //acc2
            settingsForm.TB_acc2_acc.Text = profil.accounts[1].uri;
            settingsForm.TB_acc2_pass.Text = profil.accounts[1].password;
            settingsForm.TB_acc2_proxy.Text = profil.accounts[1].proxy;
            settingsForm.TB_acc2_realm.Text = profil.accounts[1].realm;
            settingsForm.TB_acc2_registrar.Text = profil.accounts[1].registrar;
            settingsForm.CB_acc2_use.Checked = profil.accounts[1].use;
            //acc3
            settingsForm.TB_acc3_acc.Text = profil.accounts[2].uri;
            settingsForm.TB_acc3_pass.Text = profil.accounts[2].password;
            settingsForm.TB_acc3_proxy.Text = profil.accounts[2].proxy;
            settingsForm.TB_acc3_realm.Text = profil.accounts[2].realm;
            settingsForm.TB_acc3_registrar.Text = profil.accounts[2].registrar;
            settingsForm.CB_acc3_use.Checked = profil.accounts[2].use;
            //acc4
            settingsForm.TB_acc4_acc.Text = profil.accounts[3].uri;
            settingsForm.TB_acc4_pass.Text = profil.accounts[3].password;
            settingsForm.TB_acc4_proxy.Text = profil.accounts[3].proxy;
            settingsForm.TB_acc4_realm.Text = profil.accounts[3].realm;
            settingsForm.TB_acc4_registrar.Text = profil.accounts[3].registrar;
            settingsForm.CB_acc4_use.Checked = profil.accounts[3].use;

        
        
        }
    }
}